#include "UseMultiMutexBetter.h"

#include <random>

UseMultiMutexBetter::UseMultiMutexBetter() {}

void UseMultiMutexBetter::inc() {

	// random n s sleep
	default_random_engine generator;
	uniform_int_distribution<int> distribution(1, 5);

	lock(UseMultiMutexBetter::m1, UseMultiMutexBetter::m2);
	lock_guard<mutex> lgm_count(UseMultiMutexBetter::m1, adopt_lock);
	lock_guard<mutex> lgm_count2(UseMultiMutexBetter::m2, adopt_lock);

	UseMultiMutexBetter::count++;
	this_thread::sleep_for(chrono::seconds(distribution(generator)));

}

uint64_t UseMultiMutexBetter::get_count() {
	return UseMultiMutexBetter::count;
}

UseMultiMutexBetter::~UseMultiMutexBetter() {}

// define static variable
uint64_t UseMultiMutexBetter::count = 0;
mutex UseMultiMutexBetter::m1;
mutex UseMultiMutexBetter::m2;